package top.xxyu.controller;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import top.xxyu.annotation.Log;
import top.xxyu.core.Msg;
import top.xxyu.dao.MailMessageDao;
import top.xxyu.entity.MailMessage;
import top.xxyu.util.UUIDUtils;

import java.util.Objects;

@RestController
@RequestMapping(value = "mailMessage")
public class MailMessageController {
    @Autowired
    private MailMessageDao mailMessageDao;


    @ApiOperation(value = "/", httpMethod = "POST", notes = "添加邮件消息")
    @Log(desc = "添加邮箱消息")
    @PostMapping
    public Msg add(@RequestBody MailMessage mailMessage){

        if (Objects.isNull(mailMessage)) {
            return Msg.error("添加有邮件消息失败");
        }
        if(StringUtils.isBlank(mailMessage.getId())) {
            mailMessage.setId(UUIDUtils.getID());
        }
        mailMessageDao.save(mailMessage);
        return Msg.success("添加邮件消息成功");

    }

    @ApiOperation(value = "/", httpMethod = "PUT", notes = "更新邮件消息")
    @Log(desc = "更新邮箱消息")
    @PutMapping
    public Msg update(@RequestBody MailMessage mailMessage){

        if (Objects.isNull(mailMessage)) {
            return Msg.error("添加有邮件消息失败");
        }
        mailMessageDao.save(mailMessage);
        return Msg.success("添加邮件消息成功");

    }

    @ApiOperation(value = "/{page}/{size}", httpMethod = "GET", notes = "获取邮箱消息列表")
    @Log(desc = "获取邮箱消息列表")
    @GetMapping
    public Msg getAll(@PathVariable int page, @PathVariable int size){
        return Msg.success("获取邮箱消息列表",mailMessageDao.findAll(PageRequest.of(page - 1, size)));

    }

    @ApiOperation(value = "/{id}", httpMethod = "DELETE", notes = "删除邮箱消息")
    @Log(desc = "获取邮箱消息列表")
    @DeleteMapping
    public Msg delete(@PathVariable String id){
        mailMessageDao.deleteById(id);
        return Msg.success("删除邮件消息成功");

    }

}
